{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 邮件发送原理简介和准备工作"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1、邮件发送原理图\n",
    "\n",
    "<table width=\"100%\" border=\"0\">\n",
    "<tr>\n",
    "  <td valign=\"top\">\n",
    "    <img src=\"images/邮件发送原理.png\" align=\"left\"/>\n",
    "  </td>\n",
    "  <td style=\"vertical-align:top; text-align:left;\">\n",
    "      \n",
    "### 模拟场景：\n",
    "\n",
    "网易邮箱（xxxxx@163.com）发送邮件给新浪邮箱（xxxxx@sina.com）\n",
    "\n",
    "### 准备：\n",
    "\n",
    "[网易邮箱]：邮件的发送方，需要提供**邮箱<font style=\"color:red\">SMTP</font>服务器地址、邮箱帐号、密码**\n",
    "\n",
    "[新浪邮箱]：邮件的接收方，只需提供**邮箱帐号**即可\n",
    "\n",
    "1、**<font style=\"color:blue\">电脑.A</font>**根据**邮箱<font style=\"color:red\">SMTP</font>服务器地址** 连接到 **<font style=\"color:blue\">网易163邮箱的SMTP服务器.B</font>**\n",
    "\n",
    "2、**<font style=\"color:blue\">A</font>** 将 **163邮箱帐号、密码** 发给 **<font style=\"color:blue\">B</font>** 进行身份验证\n",
    "\n",
    "3、**<font style=\"color:blue\">B</font>** 验证 **163邮箱帐号和密码** 身份的合法性\n",
    "\n",
    "4、身份验证通过后，**<font style=\"color:blue\">A</font>** 再发送 **接收方新浪邮箱、邮件内容** 给**<font style=\"color:blue\">B</font>**\n",
    "\n",
    "5、**<font style=\"color:blue\">B</font>** 根据**接收方的邮箱**很容易找到接收方**<font style=\"color:blue\">新浪邮件服务器.C</font>**\n",
    "\n",
    "6、最后**<font style=\"color:blue\">B</font>**将**接收方新浪邮箱、邮件内容**发给**<font style=\"color:blue\">C</font>**即完成了邮件的发送\n",
    "\n",
    "  </td>\n",
    "</tr>\n",
    "</table>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<font style=\"color:red\">邮件发送的步骤细节还是比较多的，但 Python 代码就比较简单，等会就看到了。</font>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2、开启SMTP服务"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**由于网易邮件服务器考虑到安全性，发送邮件的“SMTP服务”默认是关闭的，需要您手动开启**\n",
    "\n",
    "<font style=\"color:blue\">下面以网易邮箱为例，开启的步骤：</font>\n",
    "\n",
    "#### 1、百度搜索“网易163 SMTP服务 开启”\n",
    "\n",
    "<img src=\"images/开启网易SMTP服务.jpg\" align=\"left\"/>\n",
    "\n",
    "\n",
    "#### 2、按照步骤开启即可\n",
    "<img src=\"images/网易SMTP开启01.jpg\" align=\"left\"/>\n",
    "\n",
    "**<font color=red>有一些身份验证的步骤，这里省去...</font>**\n",
    "\n",
    "<img src=\"images/网易SMTP开启02.jpg\" align=\"left\"/>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 开始代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入邮件工具包\n",
    "# 从（from）本地 mail.py（.py扩展名省略）文件中，导入（import）Mail 类（class）\n",
    "from mail import Mail"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建一个邮件对象，需要提供发送者的以下信息：\n",
    "\n",
    "# 发送方邮箱地址：mail_addr\n",
    "# 发送方邮箱密码：password\n",
    "# 发送方邮箱的 SMTP 服务器地址\n",
    "# 发送者的显示名称（如果省略不写，接收方看到的显示名是 xxxxx@126.com）\n",
    "mail = Mail.build_mail(mail_addr=\"xxxxx@126.com\",\n",
    "                       password=\"IPBPMGYMCKDULBMM\",\n",
    "                       smtp_server=\"smtp.126.com\",\n",
    "                       display_name=\"Python超人\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1、发送普通的文本邮件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 发送文本邮件\n",
    "# 提供 接收人的邮件地址\n",
    "# 邮件标题\n",
    "# 邮件文本内容\n",
    "mail.send_text('xxxx@yeah.net', '发送的文本邮件', \"祝您成功！\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2、发送HTML邮件"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 什么是 HTML？(超文本标记语言)\n",
    "#### 看看效果"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!DOCTYPE html>\n",
    "<html>\n",
    "<body>\n",
    "  <b>一个简单的案例</b>\n",
    "  <hr/> <!-- 这里是横线 -->\n",
    "  <font style=\"color:red\">红色文字</font>  \n",
    "</body>\n",
    "</html>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "### 这里读取一个 HTML 文件（代码不做要求，后期课程会讲解）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 读取客户名单的 HTML（有兴趣可以学习一下 HTML-超文本标记语言，做网页的语言）\n",
    "with open(r'.\\templates\\客户名单.html', 'r',encoding='UTF-8') as f:\n",
    "    html = f.read()\n",
    "print(html)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 发送 HTML 邮件\n",
    "# 提供 接收人的邮件地址\n",
    "# 邮件标题\n",
    "# 邮件 html 内容\n",
    "mail.send_html('xxxx@yeah.net', '客户表格数据', html)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3、发送有附件的邮件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "attachment1 = \"files/附件_文本文件.txt\"\n",
    "attachment2 = \"files/附件_课程二维码.png\"\n",
    "\n",
    "attachment1_path = os.path.abspath(attachment1)  # 获取附件的绝对路径\n",
    "attachment2_path = os.path.abspath(attachment2)  # 获取附件的绝对路径\n",
    "\n",
    "print(attachment1_path)\n",
    "\n",
    "mail.send_html('xxxx@yeah.net', '客户表格数据+附件', html, attachment_paths=[attachment1_path, attachment2_path])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python总结"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1、邮件发送原理\n",
    "了解邮件发送原理后，对理解参数有很大的帮助。为什么需要这些参数，大家应该非常清楚了。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2、开启SMTP服务\n",
    "一定开启SMTP服务，否则您发送邮件都会失败，而且密码不一定是登录密码。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3、Html邮件\n",
    "希望邮件内容好看的朋友，建议了解一下 Html，网上搜索 Html教程，相信大家很快就能入手。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 思考题"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 学会发送邮件后，想想能用到哪些场景？\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 注意事项：\n",
    "\n",
    "### 通过 Python 可以在短时间发送大量的邮件，我们需要注意\n",
    "\n",
    "<font color=red>1、禁止发送非法的邮件。轻则封号，重则承担法律责任</font>\n",
    "   \n",
    "<font color=red>2、邮件一旦发出，基本上无法撤回，所以发送重要的邮件前，一定要认真检查，以免造成无法挽回的后果</font>\n",
    "\n",
    "<font color=red>3、发送大量的邮件有可能会被认为是垃圾邮件，对于重要的邮件一定要通过其他联系方式再次确认，以免造成无法挽回的后果</font>"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
